package com.xcf.controller.system.companyAndDepartment;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xcf.controller.base.BaseController;
import com.xcf.service.system.companyAndDepartment.CompanyAndDepartmentService;
import com.xcf.util.PageData;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
 * 公司部门
 * @author tanwei
 *	2018.1.22
 */
@Controller
@RequestMapping(value="/companyAndDepartment")
public class CompanyAndDepartmentController extends BaseController{
	@Autowired
	private CompanyAndDepartmentService companyAndDepartmentService;
	@RequestMapping(value="/page")
	public ModelAndView companyAndDepartmentPage()throws Exception{
		ModelAndView view = new ModelAndView();
		view.setViewName("/system/companyAndDepartment/company_department_list");
		return view;
		
	}
	@RequestMapping(value="/data")
	@ResponseBody
	public String companyAndDepartmentInfo(HttpServletResponse response)throws Exception{
		
//		List<Map>list = companyAndDepartmentService.findCompanyAndDepartmentInfo();
		List<Map>company = companyAndDepartmentService.findCompany();
		
		JSONArray json = new JSONArray();
		StringBuffer str1 = new StringBuffer();
		String str2 = null;
		String strn = null;
		if(company!=null){
			JSONObject jo1 = new JSONObject();
			jo1.put("id",-1);
			jo1.put("text","重庆新长丰国际物流有限公司");
			StringBuffer str = new StringBuffer();
			StringBuffer str4 = new StringBuffer();
			for(Map m : company){
				JSONObject jo = new JSONObject();
			if(!m.get("PARENT_COMPANY_ID").equals("-1")){
			jo.put("id", m.get("COMPANY_ID"));
			jo.put("text", m.get("COMPANY_NAME"));
			List<Map>department = companyAndDepartmentService.findDepartment(m.get("COMPANY_ID").toString());
			StringBuffer str3 = new StringBuffer();
			String str5 = null;
//			StringBuffer str4 = new StringBuffer();
			for(Map mp:department){
				JSONObject jo3 = new JSONObject();
				jo3.put("id", mp.get("id"));
				jo3.put("text", mp.get("text"));
				jo3.put("statu", mp.get("statu"));
				List<Map>dep = companyAndDepartmentService.findDepartmentByParentId(mp.get("id").toString());
				jo3.put("children", dep);
				str3.append(jo3+",");
			}
//			jo.put("children", department);
//			jo1.put("children", "["+jo+"]");
//			str.append("["+jo+"]");
//			jo.put("children", str3);
			if(str3.length()>0){
			str5 = str3.substring(0, str3.lastIndexOf(","))+str3.substring(str3.lastIndexOf(",")+1, str3.length());
			}
			str4.append("{").append("\"").append("id").append("\"").append(":").append(m.get("COMPANY_ID")).append(",").append("\""+"statu"+"\"").append(":").append("\"").append(m.get("statu")).append("\"").append(",").append("\""+"text"+"\"").append(":").append("\"").append(m.get("COMPANY_NAME")).append("\"").append(",").append("\""+"children"+"\"").append(":"+"[").append(str5==null?"":str5).append("]").append("}").append(",");
//			str.append(jo+",");
			}else{
				continue;
			}
			
			}
			str1.append("[").append("{").append("\"").append("id").append("\"").append(":").append(-1).append(",").append("\""+"statu"+"\"").append(":").append("\"").append("1").append("\"").append(",").append("\""+"text"+"\"").append(":").append("\""+"重庆新长丰国际物流有限公司"+"\""+",").append("\""+"children"+"\"").append(":"+"[").append(str4).append("]").append("}").append("]");
			str2 = str1.substring(0, str1.lastIndexOf(","))+str1.substring(str1.lastIndexOf(",")+1, str1.length());
//			strn = str2.substring(0, str2.lastIndexOf(","))+str2.substring(str2.lastIndexOf(",")+1, str2.length());
			//			jo1.put("children", str);
//			json.add(str1);
			
			
		}
		
		response.setContentType("text/html; charset=utf-8");
		response.getWriter().write(str2);				
		response.getWriter().flush();
		
		
		return null;
	}
	/**
	 * 保存公司和部门信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/saveCompanyAndDepartmentInfo")
	@ResponseBody
	public String saveCompanyAndDepartmentInfo()throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		String data = pd.getString("array");

		JSONArray items = JSONArray.fromObject(data);
		for(int i=0,size=items.size();i<size;i++){
			JSONObject item = items.getJSONObject(i);
				if(item.getString("state").equals("1")){
					PageData pdq = new PageData();
					pdq.put("COMPANY_NAME", item.getString("text"));
					pdq.put("PARENT_COMPANY_ID", 1);
					companyAndDepartmentService.saveCompanyInfo(pdq);
				}else if(item.getString("state").equals("2")){
					PageData pdq = new PageData();
					pdq.put("DEPARTMENT_NAME", item.getString("text"));
					pdq.put("COMPANY_ID", item.getString("nodeParId"));
					pdq.put("PARENT_DEPARTMENT_ID", -1);
					companyAndDepartmentService.saveDepartmentInfo(pdq);
				}else if(item.getString("state").equals("3")){
					PageData pdq = new PageData();
					pdq.put("DEPARTMENT_NAME", item.getString("text"));
//					pdq.put("COMPANY_ID", item.getString("nodeParId"));
					pdq.put("PARENT_DEPARTMENT_ID", item.getString("nodeParId"));
					companyAndDepartmentService.saveDepartmentInfo(pdq);
				}
		}
		return null;
	}
	/**
	 * 删除信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteInfo")
	@ResponseBody
	public String deleteInfo()throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		if(pd.getString("state").equals("2")){
			companyAndDepartmentService.deleteCompanyInfp(pd.getString("id"));
		}else if(pd.getString("state").equals("3")){
			companyAndDepartmentService.deleteDepartmentInfo(pd.getString("id"));
		}else if(pd.getString("state").equals("4")){
			companyAndDepartmentService.deleteDepartmentInfo(pd.getString("id"));
		}
		return null;
		
	}
}
